package com.presto.directo.mvvm.view

import android.content.Intent
import android.os.Bundle
import android.view.View
import android.widget.TextView
import androidx.core.widget.doOnTextChanged
import com.presto.directo.R
import com.presto.directo.base.BaseActivity
import com.presto.directo.databinding.ActivityQuestioninfoBinding
import com.presto.directo.mvvm.viewmodel.QuestionViewModel
import com.presto.directo.mvvm.viewmodel.getViewModel
import com.presto.directo.utils.*
import ru.whalemare.sheetmenu.SheetMenu

class InfoQuestionActivity : BaseActivity() {
    override val rootView: View
        get() = binding.root

    private val binding by lazy { ActivityQuestioninfoBinding.inflate(layoutInflater) }
    private val views by lazy { listOf(binding.tvProblem2, binding.tvProblem3, binding.tvProblem4, binding.tvProblem5, binding.tvProblem6, binding.tvProblem7) }
    private val viewModel: QuestionViewModel by lazy { getViewModel() }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        Af.show_question_page.track(this)
        viewModel.infoLiveData.observe(this) {
            if (viewModel.question_1 == 1) binding.rbYes.isChecked = true else if (viewModel.question_1 == 2) binding.rbNo.isChecked = true
            showInfo(binding.tvProblem2, R.array.array_credit_history2, viewModel.question_2)
            showInfo(binding.tvProblem3, R.array.array_credit_history3, viewModel.question_3)
            showInfo(binding.tvProblem4, R.array.array_credit_history4, viewModel.question_4)
            showInfo(binding.tvProblem5, R.array.array_credit_history4, viewModel.question_5)
            showInfo(binding.tvProblem6, R.array.array_credit_history4, viewModel.question_6)
            showInfo(binding.tvProblem7, R.array.array_credit_history4, viewModel.question_7)
        }
        viewModel.uploadLiveData.observe(this) {
            Af.success_submit_questionInfo.track(this)
            startActivity(Intent(this, InfoContactActivity::class.java))
            finish()
        }
        viewModel.getInfo()
    }

    override fun initEvent() {
        binding.rgProblem1.setOnCheckedChangeListener { group, checkedId ->
            viewModel.question_1 = if (checkedId == R.id.rb_yes) 1 else 2
            updateBtn()
        }
        views.forEach {
            it.doOnTextChanged { text, start, before, count -> updateBtn() }
        }
        setClick(binding.tvProblem2, R.array.array_credit_history2)
        setClick(binding.tvProblem3, R.array.array_credit_history3)
        setClick(binding.tvProblem4, R.array.array_credit_history4)
        setClick(binding.tvProblem5, R.array.array_credit_history4)
        setClick(binding.tvProblem6, R.array.array_credit_history4)
        setClick(binding.tvProblem7, R.array.array_credit_history4)
        binding.btnNext.setOnClickListener {
            Af.click_submit_questionInfo.track(this)
            viewModel.upload()
        }
    }

    private fun setClick(textView: TextView, res: Int) {
        textView.setOnClickListener {
            SheetMenu(
                actions = resources.getStringArray(res).asList(),
                onClick = {
                    textView.text = it.title
                    when (textView) {
                        binding.tvProblem2 -> viewModel.question_2 = it.id + 1
                        binding.tvProblem3 -> viewModel.question_3 = it.id + 1
                        binding.tvProblem4 -> viewModel.question_4 = it.id + 1
                        binding.tvProblem5 -> viewModel.question_5 = it.id + 1
                        binding.tvProblem6 -> viewModel.question_6 = it.id + 1
                        binding.tvProblem7 -> viewModel.question_7 = it.id + 1
                    }
                },
                layoutProvider = MyLayoutProvider()
            ).show(this)
        }
    }


    override fun updateBtn() {
        binding.btnNext.isEnabled = viewModel.question_1 > 0 && views.all { it.text.isNotBlank() }
    }

}